#include <jni.h>
#include <string>
#include "AndroidLog.h"
#include "XYCallJava.h"
#include "XYFFmpeg.h"
#include "LogUtil.h"

#ifdef __cplusplus
extern "C" {
#endif
#include <libavformat/avformat.h>

#ifdef __cplusplus
};

#endif

JavaVM *javaVM = NULL;
XYCallJava *callJava = NULL;
XYFFmpeg *fFmpeg = NULL;
XYPlayStatus *playStatus = NULL;
pthread_t thread_start;//播放音频线程

//避免退出了后再次点击退出导致资源二次释放
bool nexit = true;

//会调用JNI_OnLoad()，而卸载时会调用JNI_UnLoad()；所以我们可以通过重写JNI_OnLoad()，
// 在JNI_OnLoad()中将函数注册到Android中，以便能通过Java访问。
extern "C"
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *vm, void *reserved) {
    jint result = -1;
    javaVM = vm;
    JNIEnv env;
    LOGCATD("print logcat");
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
        return result;
    }
    return JNI_VERSION_1_4;

}


extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1parpared(JNIEnv *env, jobject instance, jstring url_) {
    const char *url = env->GetStringUTFChars(url_, 0);
    av_register_all();
    avformat_network_init();
//    AVCodec *c_temp = av_codec_next(NULL);
    LOGE("[Other]:%s", "test");
    //TODO
    if (fFmpeg == NULL) {
        if (callJava == NULL) {
            callJava = new XYCallJava(javaVM, env, &instance);
        }

        callJava->onCallLoad(MAIN_THREAD, true);
        playStatus = new XYPlayStatus();
        fFmpeg = new XYFFmpeg(playStatus, callJava, url);
        fFmpeg->prepared();
    }


    env->ReleaseStringUTFChars(url_, url);
}


void *startCallBack(void *data) {
    XYFFmpeg *fFmpeg = (XYFFmpeg *) data;
    fFmpeg->start();
    pthread_exit(fFmpeg);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1start(JNIEnv *env, jobject instance) {

    // TODO

    if (fFmpeg != NULL) {
//        fFmpeg->start();
        pthread_create(&thread_start, NULL, startCallBack, fFmpeg);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1pause(JNIEnv *env, jobject instance) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->pause();
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1resume(JNIEnv *env, jobject instance) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->resume();
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1stop(JNIEnv *env, jobject instance) {

    // TODO

    if (!nexit) {
        return;
    }

    jclass jlz = env->GetObjectClass(instance);
    jmethodID jmid_next = env->GetMethodID(jlz, "onCallNext", "()V");

    nexit = false;
    if (fFmpeg != NULL) {
        fFmpeg->release();
        delete (fFmpeg);
        fFmpeg = NULL;
        if (callJava != NULL) {
            delete (callJava);
            callJava = NULL;
        }

        if (playStatus != NULL) {
            delete (playStatus);
            playStatus = NULL;
        }
    }
    nexit = true;
    //结束之后回调
    LOGD("结束之后调用 下一首歌")
    env->CallVoidMethod(instance, jmid_next);

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1seek(JNIEnv *env, jobject instance, jint secds) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->seek(secds);
    }

}

extern "C"
JNIEXPORT jint JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1duration(JNIEnv *env, jobject instance) {

    // TODO
    if (fFmpeg != NULL) {
        return fFmpeg->duration;
    }
    return 0;

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1volume(JNIEnv *env, jobject instance, jint percent) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->setVolume(percent);
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1mute(JNIEnv *env, jobject instance, jint mute) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->setMute(mute);
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1pitch(JNIEnv *env, jobject instance, jfloat ptich) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->setPitch(ptich);
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1speed(JNIEnv *env, jobject instance, jfloat speed) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->setSpeed(speed);
    }
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1samplerate(JNIEnv *env, jobject instance) {

    // TODO
    if (fFmpeg != NULL) {
        return fFmpeg->getSampleRate();
    }
    return 0;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1startstoprecord(JNIEnv *env, jobject instance,
                                                          jboolean start) {

    // TODO
    if (fFmpeg != NULL) {
        fFmpeg->startStopRecord(start);
    }

}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_xyyy_xyplayer_player_XYplayer_n_1cutaudioplay(JNIEnv *env, jobject instance,
                                                       jint start_time, jint end_time,
                                                       jboolean showPcm) {
    // TODO
    if (fFmpeg != NULL) {
        return fFmpeg->catAudioPlay(start_time, end_time, showPcm);
    } else {
        LOGD("ffmpeg is null")
    }
    return false;

}